package com.base.get;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class DoGET {

    public static void main(String[] args) throws Exception {

        // 创建Httpclient对象,相当于打开浏览器
        CloseableHttpClient httpclient = HttpClients.createDefault();

        // 创建http GET请求,相当于在URL地址栏输入连接
        HttpGet httpGet = new HttpGet("http://www.tianmao.com");

        CloseableHttpResponse response = null;
        try {
            // 执行请求,相当于输入回车,服务器给我们响应
            response = httpclient.execute(httpGet);
            // 判断返回状态是否为200.判断响应是否响应正常
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println("内容长度："+content.length());
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            //相当于关闭浏览器
            //httpclient.close();
        }

    }

}
